<?php



namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * 动态点赞
 */
class MomentLike extends Model
{
    protected $table = 'moment_like';

    protected $fillable = [
        'user_id', 'moment_id', 'flag'
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    /**
     * 添加点赞记录
     */
    static function addLike($userId = 0, $momentId = 0, $flag = 1)
    {
        $ret = self::updateOrCreate([
            'user_id' => $userId,
            'moment_id' => $momentId,
        ], [
            'user_id' => $userId,
            'moment_id' => $momentId,
            'flag' => $flag
        ]);
        return $ret ? 1 : 0;
    }

    /**
     * 用户是否点赞动态
     */
    static function check($userId = 0, $momentId = 0)
    {
        $where = ['user_id' => $userId, 'moment_id' => $momentId, 'flag' => 1];
        return self::where($where)->count() ? 1 : 0;
    }
}
